
/**
* @project Twitter
* @author Dayong.Shen
* @package com.ithing.database.bean
* @file XmlResult.java
* 
* @date 2014-2-25-上午10:29:54
* @Copyright 2014 ISI Team of NUDT-版权所有
* 
*/

package com.qdairlines.common;

import org.springframework.validation.BindingResult;

import java.io.Serializable;
import java.util.Date;
import java.util.List;


public class Response<T> implements Serializable {


	// 异常类型
	private String error;

	//操作结果描述
	private String message;

	// 接口调用返回状态码
	private int status;

	//返回的对象的个数，只适用于List变量
	private int numReturn;

	//操作时间
	private long timestamp;

	//返回的对象
	private T result;

	/**
	 * 默认构造方法
	 */
	public Response() {
	}

	/**
	 * validation认证失败异常构造
	 * @param bindingResult
     */
	public Response(BindingResult bindingResult) {
		this.message = bindingResult.getAllErrors().get(0).getDefaultMessage();
		this.status = -100;
		this.timestamp=new Date().getTime();
	}

	/**
	 * 自定义异常构造
	 * @param customException
     */
	public Response(CustomException customException) {
		this.setStatus(customException.getExceptionCode());
		this.setMessage(customException.getMessage());
		this.timestamp=new Date().getTime();
		this.setError(customException.getClass().getSimpleName());
	}


	public int getNumReturn() {
		return numReturn;
	}


	public void setNumReturn(int numReturn) {
		this.numReturn = numReturn;
	}


	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public int getStatus() {
		return status;
	}

	public long getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public void setStatus(int status) {
		this.status = status;
	}


	public T getResult() {
		return result;
	}

	/**
	 * 设置返回结果
	 * @param result
     */
	public void setResult(T result) {
		this.result = result;
		if (result instanceof List) {
			this.numReturn = ((List) result).size();// 如果result为列表数据，numReturn设为列表长度
		}
		setInfos(ReturnCode.No_Err);
	}

	/**
	 * 没有返回结果的情况下设置返回信息
	 * @param returnCode
     */
	public void setInfos(ReturnCode returnCode) {
		this.status = returnCode.getCode();
		this.message = returnCode.getDescription();
		this.timestamp=new Date().getTime();
		/*if (returnCode.equals(ReturnCode.Err_RequestFailed)) {// 请求失败，result设为空
			this.result = null;
		}*/
	}


}
